<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工号" prop="teacherCode">
        <el-input
          v-model="queryParams.teacherCode"
          placeholder="请输入工号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
   
      <el-form-item label="姓名" prop="realname">
        <el-input
          v-model="queryParams.realname"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
    
      
      <el-form-item label="民族" prop="ethnicity">
        <el-input
          v-model="queryParams.ethnicity"
          placeholder="请输入民族"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入职称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="籍贯" prop="nativePlace">
        <el-input
          v-model="queryParams.nativePlace"
          placeholder="请输入籍贯"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="学历" prop="education">
        <el-input
          v-model="queryParams.education"
          placeholder="请输入学历"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学位" prop="degree">
        <el-input
          v-model="queryParams.degree"
          placeholder="请输入学位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="归属部门" prop="deptId">
                     <el-tree-select
                        v-model="form.deptId"
                        :data="deptOptions"
                        :props="{ value: 'id', label: 'label', children: 'children' }"
                        value-key="id"
                        placeholder="请选择归属部门"
                        check-strictly
                        style="width: 300px"
                     />
                  </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:teacher:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:teacher:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:teacher:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:teacher:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="教师id" align="center" prop="userId" />
      <el-table-column label="工号" align="center" prop="teacherCode" />
      <el-table-column label="姓名" align="center" prop="realname" />
      <el-table-column label="用户性别" align="center" prop="sex" />
      <el-table-column label="email" align="center" prop="email" />
      <el-table-column label="出生日期" align="center" prop="birthday" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="政治面貌" align="center" prop="politicalStatus" />
      <el-table-column label="民族" align="center" prop="ethnicity" />
      <el-table-column label="手机" align="center" prop="phone" />
      <el-table-column label="职称" align="center" prop="title" />
      <el-table-column label="籍贯" align="center" prop="nativePlace" />
      <el-table-column label="住址" align="center" prop="address" />
      <el-table-column label="学历" align="center" prop="education" />
      <el-table-column label="学位" align="center" prop="degree" />
      <el-table-column label="毕业院校" align="center" prop="school" />
      <el-table-column label="所学专业" align="center" prop="major" />
      <el-table-column label="部门id" align="center" prop="depId" />
      <el-table-column label="部门名称" align="center" prop="depName" />
      <el-table-column label="银行卡" align="center" prop="bankCard" />
      <el-table-column label="开户行" align="center" prop="issuingBank" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:teacher:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:teacher:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老师管理对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="teacherRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="1">
          <el-col :span="12"> <el-form-item label="工号" prop="teacherCode">
          <el-input v-model="form.teacherCode" placeholder="请输入工号" />
        </el-form-item></el-col>
          <el-col :span="12"><el-form-item label="姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入姓名" />
        </el-form-item></el-col>
        </el-row>
        <el-row :gutter="1">
          <el-col :span="12"><el-form-item label="政治面貌" prop="politicalStatus">
         
         <el-select v-model="form.politicalStatus" placeholder="请选择">
                       <el-option
                          v-for="dict in cis_political"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.label"
                       ></el-option>
                    </el-select>
       </el-form-item></el-col>
          <el-col :span="12"> <el-form-item prop="sex" label="性别">
                     <el-select v-model="form.sex" placeholder="请选择">
                        <el-option
                           v-for="dict in sys_user_sex"
                           :key="dict.value"
                           :label="dict.label"
                           :value="dict.label"
                        ></el-option>
                     </el-select>
                  </el-form-item></el-col>
        </el-row>
        <el-row :gutter="1">
          <el-col :span="12"><el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机" />
        </el-form-item></el-col>
          <el-col :span="12"> <el-form-item label="email" prop="email">
          <el-input v-model="form.email" placeholder="请输入email" />
        </el-form-item></el-col>
        </el-row>
   
        
        
        <el-form-item label="归属部门" prop="deptId">
                     <el-tree-select
                        v-model="form.deptId"
                        :data="deptOptions"
                        :props="{ value: 'id', label: 'label', children: 'children' }"
                        value-key="id"
                        placeholder="请选择归属部门"
                        :default-expand-all="true"
                        check-strictly
                     />
                  </el-form-item>
        <el-row :gutter="1">
          <el-col :span="12"> <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item></el-col>
          <el-col :span="12">  <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item></el-col>
        </el-row>
       
        <el-row :gutter="1">
          <el-col :span="12"> <el-form-item label="民族" prop="ethnicity">
            <el-select
               v-model="form.ethnicity"
               placeholder="请选择民族"
               clearable
               style="width: 200px"
               
            >
               <el-option
                  v-for="item in national"
                  :key="item"
                  :label="item"
                  :value="item"
               />
            </el-select>
         </el-form-item></el-col>
          <el-col :span="12"> 
        <el-form-item prop="title" label="职称">
                     <el-select v-model="form.title" placeholder="请选择职称">
                        <el-option
                           v-for="dict in cis_teacher_title"
                           :key="dict.value"
                           :label="dict.label"
                           :value="dict.label"
                        ></el-option>
                     </el-select>
                  </el-form-item></el-col>
        </el-row>
        
        <el-row :gutter="1">
          <el-col :span="12"><el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
        </el-form-item></el-col>
          <el-col :span="12"><el-form-item label="住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入住址" />
        </el-form-item></el-col>
        </el-row>
       
        <el-row :gutter="1">
          <el-col :span="12"> <el-form-item label="学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历" />
        </el-form-item></el-col>
          <el-col :span="12"><el-form-item label="学位" prop="degree">
          <el-input v-model="form.degree" placeholder="请输入学位" />
        </el-form-item></el-col>
        </el-row>
        
        <el-row :gutter="1">
          <el-col :span="12"><el-form-item label="毕业院校" prop="school">
          <el-input v-model="form.school" placeholder="请输入毕业院校" />
        </el-form-item></el-col>
          <el-col :span="12"> <el-form-item label="所学专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入所学专业" />
        </el-form-item></el-col>
        </el-row>
        
        <el-row :gutter="1">
          <el-col :span="12"><el-form-item label="银行卡" prop="bankCard">
          <el-input v-model="form.bankCard" placeholder="请输入银行卡" />
        </el-form-item></el-col>
          <el-col :span="12"><el-form-item label="开户行" prop="issuingBank">
          <el-input v-model="form.issuingBank" placeholder="请输入开户行" />
        </el-form-item></el-col>
        </el-row>
       
    
        
        
        
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Teacher">
import { listTeacher, getTeacher, delTeacher, addTeacher, updateTeacher } from "@/api/infomanagement/teacher";
import { deptTreeSelect } from "@/api/system/user";

const { proxy } = getCurrentInstance();
const {  sys_user_sex,cis_political,cis_household,cis_teacher_title } = proxy.useDict("sys_user_sex","cis_political","cis_household","cis_teacher_title");

const teacherList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptName = ref("");
const deptOptions = ref(undefined);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    teacherCode: null,
    campusId: null,
    realname: null,
    sex: null,
    email: null,
    birthday: null,
    idCard: null,
    politicalStatus: null,
    ethnicity: null,
    phone: null,
    title: null,
    nativePlace: null,
    address: null,
    education: null,
    degree: null,
    school: null,
    major: null,
    collegeId: null,
    collegeName: null,
    deptId: null,
    depName: null,
    bankCard: null,
    issuingBank: null,
    photo: null,
    status: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};
/** 查询老师管理列表 */
function getList() {
  loading.value = true;
  listTeacher(queryParams.value).then(response => {
    teacherList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    userId: null,
    teacherCode: null,
    campusId: null,
    realname: null,
    sex: null,
    email: null,
    birthday: null,
    idCard: null,
    politicalStatus: null,
    ethnicity: null,
    phone: null,
    title: null,
    nativePlace: null,
    address: null,
    education: null,
    degree: null,
    school: null,
    major: null,
    collegeId: null,
    collegeName: null,
    depId: null,
    depName: null,
    bankCard: null,
    issuingBank: null,
    photo: null,
    status: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("teacherRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加老师管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _userId = row.userId || ids.value
  getTeacher(_userId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改老师管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["teacherRef"].validate(valid => {
    if (valid) {
      proxy.$modal.loading("正在提交中...");
      if (form.value.userId != null) {
        updateTeacher(form.value).then(response => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      } else {
        addTeacher(form.value).then(response => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除老师管理编号为"' + _userIds + '"的数据项？').then(function() {
    return delTeacher(_userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('infomanagement/teacher/export', {
    ...queryParams.value
  }, `teacher_${new Date().getTime()}.xlsx`)
}
getDeptTree();
getList();
const national = ["汉族", "壮族", "满族", "回族", "苗族", "维吾尔族", "土家族", "彝族", "蒙古族", "藏族", "布依族", "侗族", "瑶族", "朝鲜族", "白族", "哈尼族","哈萨克族", "黎族", "傣族", "畲族", "傈僳族", "仡佬族", "东乡族", "高山族", "拉祜族", "水族", "佤族", "纳西族", "羌族", "土族", "仫佬族", "锡伯族",
            "柯尔克孜族", "达斡尔族", "景颇族", "毛南族", "撒拉族", "布朗族", "塔吉克族", "阿昌族", "普米族", "鄂温克族", "怒族", "京族", "基诺族", "德昂族", "保安族","俄罗斯族", "裕固族", "乌孜别克族", "门巴族", "鄂伦春族", "独龙族", "塔塔尔族", "赫哲族", "珞巴族"]
</script>

